package com.personalblog.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.personalblog.po.Blog;
import com.personalblog.po.Type;
import com.personalblog.service.BlogService;
import com.personalblog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author jxau_scf
 * @date 2021/3/30 - 10:21
 */
@Controller
public class TypeShowController {
    @Autowired
    private TypeService typeService;
    @Autowired
    private BlogService blogService;

    @GetMapping("/types/{id}")
    public String types(@RequestParam(defaultValue = "1") int page, @PathVariable int id, Model model){
        List<Type> typeList = typeService.findTypesFetchBlogs();
        if (id == -1){
            id = typeList.get(0).getTypeId();
        }
        String orderBy = "blog_update_time desc";
        PageHelper.startPage(page,5,orderBy);
        List<Blog> blogList = blogService.findBlogsByTypeId(id);
        PageInfo pageInfo = new PageInfo(blogList);
        model.addAttribute("typeList",typeList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("activeTypeId",id);
        return "types";
    }
}
